<template>
  <div id="app">
    <header>
      <nav>
        <div class="logo">CFMOTO 春风动力</div>
        <ul class="nav-links">
          <li><router-link to="/">首页</router-link></li>
          <li><router-link to="/motorcycle">摩托车</router-link></li>
          <li><router-link to="/gear">骑行装备</router-link></li>
          <li><router-link to="/parts">配件改装</router-link></li>
          <li>
            <router-link to="/cart" class="cart-link">
              购物车
              <span v-if="cartItemCount > 0" class="cart-count">{{ cartItemCount }}</span>
            </router-link>
          </li>
        </ul>
        <ul class="user-links">
          <li v-if="!isLoggedIn"><router-link to="/login">登录</router-link></li>
          <li v-if="!isLoggedIn"><router-link to="/register">注册</router-link></li>
          <li v-if="isLoggedIn" class="user-info">
            <span>{{ currentUser }}</span>
            <button @click="logout">退出</button>
          </li>
        </ul>
      </nav>
    </header>
    <main>
      <router-view />
    </main>
    <footer>
      <p>© 2025 CFMOTO 春风动力 版权所有</p>
    </footer>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useStore } from 'vuex'

export default {
  name: 'App',
  setup() {
    const store = useStore()
    const isLoggedIn = ref(localStorage.getItem('isLoggedIn') === 'true')
    const currentUser = ref(localStorage.getItem('currentUser'))
    const cartItemCount = computed(() => store.getters.cartItemCount)

    const logout = () => {
      localStorage.removeItem('isLoggedIn')
      localStorage.removeItem('currentUser')
      isLoggedIn.value = false
      currentUser.value = ''
      store.commit('clearCart')
      window.location.href = '/'
    }

    return {
      cartItemCount,
      isLoggedIn,
      currentUser,
      logout
    }
  }
}
</script>

<style>
:root {
  --cf-blue: #0078A0;
  --cf-light-blue: #d4eaf7;
  --cf-white: #ffffff;
  --cf-dark-blue: #005c80;
  --cf-gray: #666666;
  --cf-light-gray: #f5f5f5;
}

#app {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: var(--cf-gray);
  margin: 0;
  padding: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: var(--cf-light-blue);
}

header {
  background-color: var(--cf-blue);
  color: var(--cf-white);
  padding: 15px 0;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.logo {
  font-size: 24px;
  font-weight: bold;
  color: var(--cf-white);
  text-decoration: none;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.nav-links, .user-links {
  list-style-type: none;
  margin: 0;
  padding: 0;
  display: flex;
  align-items: center;
}

.nav-links li, .user-links li {
  margin: 0 15px;
}

.nav-links a, .user-links a {
  color: var(--cf-white);
  text-decoration: none;
  font-weight: 500;
  padding: 5px 10px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.nav-links a:hover, .user-links a:hover {
  color: var(--cf-white);
  background-color: var(--cf-dark-blue);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-info button {
  background-color: transparent;
  color: var(--cf-white);
  border: 1px solid var(--cf-white);
  padding: 5px 15px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.user-info button:hover {
  background-color: var(--cf-white);
  color: var(--cf-blue);
}

main {
  flex: 1;
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

footer {
  background-color: var(--cf-blue);
  color: var(--cf-white);
  padding: 20px;
  text-align: center;
}

footer p {
  color: var(--cf-white);
  margin: 0;
}

@media (max-width: 768px) {
  nav {
    flex-direction: column;
    gap: 15px;
  }

  .nav-links, .user-links {
    flex-wrap: wrap;
    justify-content: center;
  }

  .nav-links li, .user-links li {
    margin: 5px;
  }
}

.cart-link {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.cart-count {
  position: absolute;
  top: -8px;
  right: -12px;
  background-color: var(--cf-white);
  color: var(--cf-blue);
  border-radius: 50%;
  padding: 2px 6px;
  font-size: 12px;
  font-weight: bold;
  min-width: 18px;
  text-align: center;
}
</style>